<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
header('Content-type: application/json');
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
ini_set('log_errors', '1');
ini_set('error_log', 'php_error.log');
ini_set('display_errors', '1');

include_once("file.common.php");

$skipChecks = FALSE;
authenticate($validCredentials);

$result = TRUE;
$srcDir = "../";
$dstDir = "../pages";
$exclude = array("db", "bk");
$msg = '';
$log = array();
$totalDirs = 0;
$totalFiles = 0;
$totalSize = 0;

/** 
 * Recursively delete a directory 
 * 
 * @param string $dir Directory name 
 * @param boolean $deleteRootToo Delete specified top-level directory as well 
 */ 
function RoboDelete($dir, $deleteRootToo) 
{ 
    if(!$dh = @opendir($dir)) 
    { 
        return false;
    } 
    while (false !== ($obj = readdir($dh))) 
    { 
        if($obj == '.' || $obj == '..') 
        { 
            continue; 
        } 

        if (!@unlink($dir . '/' . $obj)) 
        { 
            unlinkRecursive($dir.'/'.$obj, true); 
        } 
    } 

    closedir($dh); 
    
    if ($deleteRootToo) 
    { 
        return @rmdir($dir); 
    } 
    
    return true; 
} 

/** 
 * Recursively duplicate a file or directory with file and folder exclusions
 */ 
function RoboCopy( $source, $destination, $exclude ) {
	global $log, $totalFiles, $totalDirs, $totalSize;
	if (!$exclude)
		$exclude = array();
	if ( is_dir( $source ) ) {
		$pinfo = pathinfo($source);
		if (in_array($pinfo['basename'], $exclude))
		{
			$log["exclude: $source"] = $pinfo['basename'];
			return;
		}
		//@mkdir( $destination); // use this one for logging only, no creates
		@mkdir( $destination, 0777, true );
		$log["mkdir: $source"] = "$destination";
		$totalDirs += 1;
		$directory = dir( $source );
		while ( FALSE !== ( $item = $directory->read() ) ) {
			if ( $item == '.' || $item == '..' ) {
				continue;
			}
			$PathDir = $source . '/' . $item; 
			if ( is_dir( $PathDir ) ) {
				RoboCopy( $PathDir, $destination . '/' . $item, $exclude );
				continue;
			}
			$pinfo = pathinfo($PathDir);
			if (in_array($pinfo['basename'], $exclude))
			{
				$log["exclude: $PathDir"] = "$destination/$item";
				continue;
			}
			@copy( $PathDir, $destination . '/' . $item );
			$log["copy: $PathDir"] = "$destination/$item";
			$totalSize += filesize("$destination/$item");
			$totalFiles += 1;
		}
 
		$directory->close();
	} else {
		$pinfo = pathinfo($source);
		if (in_array($pinfo['basename'], $exclude))
		{
			$log["exclude: $source"] = $pinfo['basename'];
			return;
		}
		@copy( $source, $destination );
		$log["copy: $source"] = "$destination";
		$totalSize += filesize("$destination");
		$totalFiles += 1;
	}
}

// Copy data tree over to pages tree to create a static snapshot of it
@mkdir("$dstDir/$appPath", 0777, true);
RoboDelete("$dstDir/$appPath/sc");
RoboDelete("$dstDir/$appPath/md");
RoboDelete("$dstDir/$appPath/cs");
RoboCopy("$srcDir/$appPath", "$dstDir/$appPath", $exclude);
RoboCopy("$srcDir/Scripts", "$dstDir/Scripts", $exclude);

if ($callback)
	$ee .= "$callback({\n";
else
	$ee .= "{\n";
$ee .= '"result":' . json_encode($result) . ",\n";
$ee .= '"srcDir":' . json_encode($srcDir) . ",\n";
$ee .= '"dstDir":' . json_encode($dstDir) . ",\n";
$ee .= '"totalDirs":' . json_encode($totalDirs) . ",\n";
$ee .= '"totalFiles":' . json_encode($totalFiles) . ",\n";
$ee .= '"totalSize":' . json_encode($totalSize) . ",\n";
$ee .= '"msg":' . json_encode($msg) . ",\n";
$ee .= '"log":' . json_encode($log) . ",\n";
$ee .= '"end":true' . "\n";
if ($callback)
	$ee .= "})";
else
	$ee .= "}";
echo $ee;

?>